package com.byt.xmxb.activity;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

import com.billy.cc.core.component.CC;
import com.billy.cc.core.component.CCResult;
import com.byt.base.AppManager;
import com.byt.base.activity.BaseActivity;
import com.byt.base.fragment.BaseFragment;
import com.byt.base.viewmodel.BaseViewModel;
import com.byt.common.adapter.TabFragmentAdapter;
import com.byt.xmxb.R;
import com.byt.xmxb.databinding.ActivityMainBinding;
import com.byt.xmxb.fragment.HomeFragment;
import com.byt.xmxb.fragment.MyInfoFragment;

import java.util.ArrayList;

import androidx.annotation.RequiresApi;

public class MainActivity extends BaseActivity<ActivityMainBinding, BaseViewModel> implements View.OnClickListener {
    private ArrayList<BaseFragment> fragmentList = new ArrayList<>();
    private TabFragmentAdapter mAdapters;
    private BaseFragment consultFragment = null;
    private int position = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        SetTranslanteBar();
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        super.onCreate(savedInstanceState);
        initWidget();
    }

    @RequiresApi(api = Build.VERSION_CODES.ECLAIR)
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {
            moveTaskToBack(false);
            AppManager.getAppManager().finishAllContext();
            overridePendingTransition(R.anim.anim_in, R.anim.anim_out);
        }
        return super.onKeyDown(keyCode, event);
    }

    @SuppressLint("NewApi")
    private void initWidget() {
        CCResult result = CC.obtainBuilder("Consult").setActionName("getConsultFragment")
                .build().call();
        consultFragment = (BaseFragment) result.getDataMap().get("fragment");
        fragmentList.add(consultFragment);
        fragmentList.add(HomeFragment.newsInstance());
        fragmentList.add(HomeFragment.newsInstance());
        fragmentList.add(MyInfoFragment.newsInstance());
        mAdapters = new TabFragmentAdapter(getSupportFragmentManager(), fragmentList);
        dataBinding.nsvpMainContext.setAdapter(mAdapters);
        dataBinding.nsvpMainContext.setOffscreenPageLimit(fragmentList.size());
        dataBinding.nsvpMainContext.setCurrentItem(position, false);
        setCurrentSelect(position);
        dataBinding.rlMainHome.setOnClickListener(this);
        dataBinding.rlMainPatient.setOnClickListener(this);
        dataBinding.rlMainFamous.setOnClickListener(this);
        dataBinding.rlMainMyinfo.setOnClickListener(this);
    }

    private void setCurrentSelect(int current) {
        dataBinding.imgMainHome.setSelected(false);
        dataBinding.rlMainPatient.setSelected(false);
        dataBinding.rlMainFamous.setSelected(false);
        dataBinding.imgMainMyinfo.setSelected(false);
        switch (current) {
            case 0:
                dataBinding.imgMainHome.setSelected(true);
                break;
            case 1:
                dataBinding.rlMainPatient.setSelected(true);
                break;
            case 2:
                dataBinding.rlMainFamous.setSelected(true);
                break;
            case 3:
                dataBinding.imgMainMyinfo.setSelected(true);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.rl_main_home) {
            dataBinding.nsvpMainContext.setCurrentItem(0, false);
            setCurrentSelect(0);
        } else if (id == R.id.rl_main_patient) {
            dataBinding.nsvpMainContext.setCurrentItem(1, false);
            setCurrentSelect(1);
        } else if (id == R.id.rl_main_famous) {
            dataBinding.nsvpMainContext.setCurrentItem(2, false);
            setCurrentSelect(2);
        } else if (id == R.id.rl_main_myinfo) {
            dataBinding.nsvpMainContext.setCurrentItem(3, false);
            setCurrentSelect(3);
        }
    }

    @Override
    protected void onRetryBtnClick() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }


}
